/**
 * zmn文章模块-文章编辑
 * 文件命名格式：zmn.模块.表/功能.jas
 * @author heguanghua
 * @since 2018/10/18
 */
var form = null;

layui.define(['form', 'layedit', 'laydate', 'zmnBind', 'upload'], function (exports) {
    let $ = layui.$;
    form = layui.form;
    let layer = layui.layer;
    let laydate = layui.laydate;
    let zmnBind = layui.zmnBind;
    let index;

    let contractBorrowRecordVO = json.contractBorrowRecordVO;


    let staffIdSearch = {
        init: function () {
            let searchValue = null;
            zmnBind.searchOperator("staffId", function () {
                return {status: 2};
            }, true, false, null, null, function(values){
            });
            $('#staffId .xm-search-input').on('input propertychange', function () {
                searchValue = $('#borrower .xm-search-input').val();
            });
        }
    };

    staffIdSearch.init();

    zmnLayUtil.layFormVerify(form);

    form.render(null, "zmn-form");

    // 提交
    form.on('submit(zmn-form-submit)', function (obj) {
        let data = obj.field;
        if (data.staffId == null || data.staffId == undefined || data.staffId == '') {
            layer.msg("员工账号ID/姓名不能为空！");
            return false;
        }

        let url = "/msg/conf/receive/staff/addEdit.action";
        var openId = layer.open({type: 3});
        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            success:function(result) {
                layer.close(openId);
                if(result.status == 200) {
                    layer.msg(result.message,{time: 1000},()=>{
                        zmnLayUtil.closeThisIframe(parent);
                        parent.location.reload();
                    });
                } else {
                    layer.msg(result.message, {"icon": 2});
                }
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
                layer.close(openId);
            }
        });

        return false;
    });

    // 2级部门联动
    form.on('select(oneDeptId)', function (contractBorrowRecordVO) {
        triggerTwoDeptOptions(contractBorrowRecordVO.value);
    })

    function triggerTwoDeptOptions(value) {
        let twoDeptOptions = json.twoDepts.filter(p => p.parentId == value).map(p => {
            return p.deptId == contractBorrowRecordVO.agentdeptTwoId ? '<option value="' + p.deptId + '" selected="selected">' + p.deptName + '</option>' : '<option value="' + p.deptId + '">' + p.deptName + '</option>';
        });
        twoDeptOptions.unshift('<option value=""></option>');
        $('#deptId').html(twoDeptOptions);
        form.render("select");
    }

    //点击取消
    $(document).on('click', '#cancel', function () {
        let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index);
    });

    //对外暴露的接口
    exports('msg_conf_receive_staff_edit_js', {});
});

